<?php


namespace App\Http\Controllers\Api;


use App\Jobs\TopicImgJob;
use App\Models\WxDegreeIncrement;
use App\Wen\Utils\Settings;
use App\Http\Controllers\Api\Repositories\WxTagsRepositories;
use App\Models\Posts\WxTag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class TagsController extends BaseController
{

    /**
     * 热门话题列表
     * @param Request $request
     */
    public function hotTags(Request $request)
    {
        $data = WxTagsRepositories::hot_tags(39, ['id','tags_name']);
        return $this->success($data);
    }

    /** 话题info
     * @param Request $request
     */
    public function tagInfo(Request $request){
        $uid = $request->uid;
        $tagId = $request->id;
        if(_empty_($tagId)){
            return $this->fail(200001);
        }

        $tag = WxTag::find($tagId);
        if(_empty_($tag)){
            return $this->fail(200003);
        }
        $is_no_img = false;

        if(_empty_($tag->head_portrait)){
            $is_no_img = true;
            $tag->head_portrait = Settings::get('img_default_topic_avatar');
        }
        if(_empty_($tag->background_maps)){
            $is_no_img = true;
            $tag->background_maps = Settings::get('img_default_topic_bg');
        }
        if(_empty_($tag->pc_background_maps)){
            $tag->pc_background_maps = Settings::get('img_default_topic_pc_bg');
        }
        if(_empty_($tag->tag_introduce)){
            $tag->tag_introduce = '这是一个很神秘的话题...';
        }
        global $__MINI_GLOBAL_PAYCODE_12__;
        if($__MINI_GLOBAL_PAYCODE_12__){
            $tag->tags_number = (int)(Cache::remember('tags:degree:'.$tag, 3600, function () use (&$tag){
                return (int)(WxDegreeIncrement::where([['type', '=', 5], ['object_id', '=', $tag->id]])->sum('degree_increment'));
            }));
        }

        if($is_no_img){
            TopicImgJob::dispatch($tag->id);
        }
        if($uid > 0){
            update_user_visit($uid, 5, $tagId);
        }
        return $this->success($tag);

    }

}
